package com.wcsy.myblog.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcsy.myblog.dao.AnnouncementMapper;
import com.wcsy.myblog.dao.UserMapper;
import com.wcsy.myblog.entity.Announcement;
import com.wcsy.myblog.entity.User;
import com.wcsy.myblog.service.IAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements IAnnouncementService {
    @Autowired
    AnnouncementMapper announcementMapper;
    @Autowired
    HttpSession session;
    @Autowired
    UserMapper userMapper;
    @Override
    public void insertAnnouncement(String announcementContent) {
        Date date = new Date(System.currentTimeMillis());
        String format = new SimpleDateFormat("YYYY年MM月dd日HH:mm").format(date);
        User user = (User) session.getAttribute("user");
        Announcement announcement = new Announcement();
        announcement.setUserId(user.getUserId());
        announcement.setAnnouncementContent(announcementContent);
        announcement.setAnnouncement(format);
        announcementMapper.insertAnnouncement(announcement);
        //所有用户未查看的公告数+1
        userMapper.uodateAnnouncement();
    }

    @Override
    public void updateAnnouncement(String announcementContent , int announcementId) {
        Date date = new Date(System.currentTimeMillis());
        String format = new SimpleDateFormat("YYYY年MM月dd日HH:mm").format(date);
        User user = (User) session.getAttribute("user");
        Announcement announcement = new Announcement();
        announcement.setUserId(user.getUserId());
        announcement.setAnnouncementContent(announcementContent);
        announcement.setAnnouncement(format);
        announcement.setAnnouncementId(announcementId);
       announcementMapper.updateAnnouncement(announcement);
    }
}
